import { OptionsCallback } from '@/models/login'
import * as API from '@/service/tool/historyMessageApi'
import { awaitTo } from '@/utils'
import {
  CommunicateRecords,
  CourseModel,
  ParentDetailsModel,
  ParentListModel,
  StudentCoursesNumModel,
} from '@/models/historyMessageModels'

interface IState {
  parentListModelList: ParentListModel[]
  communicateRecords: CommunicateRecords[]
  parentDetails: ParentDetailsModel
  courseModelList: CourseModel[]
  studentCoursesNumModel: StudentCoursesNumModel
}

const parentDetails: ParentDetailsModel = {
  avatar: '',
  id: 0,
  mobile: '',
  nickname: '',
  realname: '',
  student: [],
  user_id: 0,
}
const historyMessageHooks = (options?: OptionsCallback) => {
  const state = reactive<IState>({
    parentListModelList: [],
    communicateRecords: [],
    parentDetails,
    courseModelList: [],
    studentCoursesNumModel: {
      online_num: 0,
      offline_num: 0,
      bookstore_num: 0,
    },
  })

  /** 家长列表 */
  async function crmContacts(keyword: string, page: number, perPage: number) {
    const { result } = await awaitTo(API.crmContacts({ keyword, page, per_page: perPage }))
    if (result) {
      return result.data
    }
  }

  /** 家长详情 */
  async function contactDetail(id: number, onFinish?: () => void) {
    const { result } = await awaitTo(API.contactDetail({ id }))
    if (result) {
      state.parentDetails = result.data
      onFinish()
    }
  }

  /** 历史沟通记录 */
  async function crmRecords(
    studentId: number,
    mobile: string,
    isOnlyShowMine: number,
    page: number,
    perPage: number,
  ) {
    const { result } = await awaitTo(
      API.crmRecords({
        student_id: studentId,
        mobile,
        is_only_show_mine: isOnlyShowMine,
        page,
        per_page: perPage,
      }),
    )
    /*    if (result) {
          state.communicateRecords = result.data.data
        } */
    return result?.data?.data || []
  }

  /** 网校课程列表 */
  async function onlineCourses(
    studentId: number,
    isOnlyShowMine: number,
    page: number,
    perPage: number,
  ) {
    const { result } = await awaitTo(
      API.onlineCourses({
        student_id: studentId,
        is_only_show_mine: isOnlyShowMine,
        page,
        per_page: perPage,
      }),
    )
    /*    if (result) {
          state.courseModelList = result.data.data
        } */
    return result?.data.data
  }

  /** 学校课程列表 */
  async function offlineCourses(
    studentId: number,
    isOnlyShowMine: number,
    page: number,
    perPage: number,
  ) {
    const { result } = await awaitTo(
      API.offlineCourses({
        student_id: studentId,
        is_only_show_mine: isOnlyShowMine,
        page,
        per_page: perPage,
      }),
    )
    /* if (result) {
       state.courseModelList = result.data.data
     } */
    return result?.data.data
  }

  /** 书店课程列表 */
  async function bookstoreCourses(
    studentId: number,
    isOnlyShowMine: number,
    page: number,
    perPage: number,
  ) {
    const { result } = await awaitTo(
      API.bookstoreCourses({
        student_id: studentId,
        is_only_show_mine: isOnlyShowMine,
        page,
        per_page: perPage,
      }),
    )
    /*   if (result) {
         state.courseModelList = result.data.data
       } */
    return result?.data.data
  }

  /** 获取学生课程数 */
  async function studentCoursesNum(studentId: number, onFinish: () => void) {
    const { result } = await awaitTo(
      API.studentCoursesNum({
        student_id: studentId,
      }),
    )
    if (result) {
      state.studentCoursesNumModel = result.data
      onFinish()
    }
  }

  return {
    ...toRefs(state),
    crmContacts,
    crmRecords,
    contactDetail,
    onlineCourses,
    offlineCourses,
    bookstoreCourses,
    studentCoursesNum,
  }
}

export default historyMessageHooks
